Page({
    data: {
      role: 'student'
    },
  
    selectRole: function(e) {
      this.setData({
        role: e.currentTarget.dataset.role
      });
    },
  
    handleLogin: function() {
      const that = this;
      wx.login({
        success(res) {
          if (res.code) {
            // 发送 code 到后台换取 token
            wx.request({
              url: `${getApp().globalData.baseUrl}/api/auth/login`,
              method: 'POST',
              data: {
                code: res.code,
                role: that.data.role
              },
              success(res) {
                if (res.data.success) {
                  // 保存token和用户角色
                  wx.setStorageSync('token', res.data.token);
                  wx.setStorageSync('userRole', that.data.role);
                  
                  // 跳转到课程表页面
                  wx.reLaunch({
                    url: '/pages/schedule/schedule'
                  });
                } else {
                  wx.showToast({
                    title: res.data.message,
                    icon: 'none'
                  });
                }
              }
            });
          }
        }
      });
    }
  });